package dao;

import beans.User;
import cn.hutool.crypto.digest.DigestUtil;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import utils.JDBCUtils;

import java.sql.SQLException;

/**
 * @author 王俊彪
 * @version 1.0
 */
public class UserDao {
    /**
     *
     * @param user service 层传递过来的用户信息
     * @return 返回数据库查询的结果，如果用户名密码正确返回对象，否则没有查询到返回null
     */
    public User login(User user){
        User existUser = null;
        QueryRunner qr = new QueryRunner(JDBCUtils.getDataSource());
        try {
            existUser = qr.query("SELECT * FROM `user` WHERE username=? AND password = ?",
                    new BeanHandler<User>(User.class),
                    user.getUsername(), DigestUtil.md5Hex(user.getPassword()));
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
        return existUser;
    }
}
